# coding: utf-8

from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.http import HttpResponse
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from jd_asset.models import AssetInfo
from .tools import *


@defend_attack
def Login(request):
    error = ''
    username = request.POST.get('username', '')
    password = request.POST.get('password', '')
    next_url = request.POST.get('next', '')
    user = authenticate(username=username, password=password)
    if user is not None and user.is_active:
            login(request, user)
            request.session.set_expiry(3600)
            return HttpResponseRedirect(request.GET.get('next', '/'))
    elif request.method == 'GET' :
        return my_render('login.html', locals(), request)
    else:
        error = u'无效的用户名或密码'
        return my_render('login.html', {'error': error}, request)


def Logout(request):
    logout(request)
    return HttpResponseRedirect(reverse('login'))


@login_required
def index(request):
    asset_all = AssetInfo.objects.all()
    return my_render('index.html', locals(), request)


def skin_config(request):
    return render_to_response('skin_config.html')


def tmp(request, t):
    return HttpResponse(t)
